using Godot;
using System;
using System.Security.Cryptography.X509Certificates;

public partial class PlayerAttackState : PlayerState
{
    [Export] private Timer comoTimerNode;
    private int comboCounter = 1;
    private int maxComboCount = 2;


    public override void _Ready()
    {
        base._Ready();

        comoTimerNode.Timeout += () => comboCounter = 1;
    }
    protected override void EnterState()
    {
        characterNode.AnimPlayerNode.Play(
            GameConstants.ANIM_ATTACK + comboCounter,
            -1,
            1.5f
        );

        characterNode.AnimPlayerNode.AnimationFinished += HandleAnimationFinished;
    }

    protected override void ExitState()
    {
        characterNode.AnimPlayerNode.AnimationFinished -= HandleAnimationFinished;
        comoTimerNode.Start();
    }

    private void HandleAnimationFinished(StringName animName)
    {
        comboCounter++;

        comboCounter = Mathf.Wrap(comboCounter,1,maxComboCount + 1);
        
        characterNode.ToggleHitbox(true);
        

        characterNode.StateMachineNode.SwitchState<PlayerIdleState>();
    }

    private void PerformHit()
    {
        Vector3 newPosition = characterNode.SpriteNode.FlipH ?
                Vector3.Left : 
                Vector3.Right;
        float distanceMultiplier = 0.75f;
        newPosition *= distanceMultiplier;

        characterNode.HitboxNode.Position = newPosition;
        characterNode.ToggleHitbox(false);

    }
}
